import { defineStore } from 'pinia'

export interface IUserInfo {
  username: string,
  position: string,
  avatar: string,
  userId: number,
  account: string,
}
interface IState {
  username: string,
  access_token: string | null,
  userInfo: IUserInfo
}

const useUserStore = defineStore('user', {
  persist: true,
  state: ():IState => {
    return {
      username: 'admin',
      access_token: '',
      userInfo: {
        username: '',
        position: '',
        avatar: '',
        userId: 0,
        account: '',
      }
    }
  },
  actions: {
    setToken(token: string) {
      this.access_token = token
    },
    setUserInfo(userInfo: IUserInfo) {
      console.log('userInfo', userInfo)
      this.userInfo = Object.assign(this.userInfo,userInfo)
    }
  },
  getters: {
    isLogin():boolean {
      if(!this.access_token) return false
      if(this.access_token !== 'undefined') return true
      return false
    }
  }
})

export default useUserStore